package ga.wavaplayer.ui {
	
	import ga.wavaplayer.ui.buttons.WButton;
	import ga.wavaplayer.ui.buttons.WDualStateButton;
	import ga.wavaplayer.ui.buttons.WIconButton;
	import ga.wloader.WLoader;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	import ga.wemanager.WEManager;
	import ga.wavaplayer.video.WVideo;
	
	public class WControls extends Sprite{
		
		private var _bg:Sprite = new Sprite();
		private var _colors:Object;
		private var _config:Object;
		
		public static const PLAY_CLICKED:String = "wcontrols.play_clicked";
		public static const PAUSE_CLICKED:String = "wcontrols.pause_clicked";
		public static const STOP_CLICKED:String = "wcontrols.stop_clicked";
		public static const MAXIMIZE_CLICKED:String = "wcontrols.maximize_clicked";
		public static const MINIMIZE_CLICKED:String = "wcontrols.minimize_clicked";
		public static const SOUND0_CLICKED:String = "wcontrols.sound0_clicked";
		public static const SOUND1_CLICKED:String = "wcontrols.sound1_clicked";
		public static const VOLUME_UPDATED:String = "wcontrols.volume_updated";
		public static const SEEK_REQUEST:String = "wcontrols.seek_request";
		public static const ICON_CLICKED:String = "wcontrols.icon_clicked";
		
		public var playBtn:WButton;
		public var pauseBtn:WButton;
		public var stopBtn:WButton;
		public var maximizeBtn:WButton;
		public var minimizeBtn:WButton;
		public var sound0Btn:WButton;
		public var sound1Btn:WButton;
		
		public var playpauseButton:WDualStateButton;
		public var fullscreenButton:WDualStateButton;
		public var soundButton:WDualStateButton;
		
		public var volumeBar:WVolumeBar;
		public var seekBar:WSeekBar;
		public var timeBox:WTimeBox;
		
		public function WControls(ui_config:Object) {
			
			//	Set Variables
			_config = ui_config.controls;
			_colors = ui_config.colors;
			
			var loader:WLoader = WLoader.getLoader("WAVAPlayer");
			
			WEManager.addEventListener(WVideo.STARTED, _videoStartedHandler);
			WEManager.addEventListener(WVideo.PAUSED, _videoPausedHandler);
			WEManager.addEventListener(WVideo.RESUMED, _videoResumedHandler);
			WEManager.addEventListener(WVideo.SOUND_DISABLED, _soundDisabledHandler);
			WEManager.addEventListener(WVideo.SOUND_ENABLED, _soundEnabledHandler);
			WEManager.addEventListener(WControls.VOLUME_UPDATED, _volumeUpdatedHandler);
			
			//	Setup Buttons
			playBtn = new WIconButton(_config, loader.getRequest("icon-play").bitmap, _colors.semilight, _colors.light, WControls.PLAY_CLICKED);
			pauseBtn = new WIconButton(_config, loader.getRequest("icon-pause").bitmap, _colors.semilight, _colors.light, WControls.PAUSE_CLICKED);
			stopBtn = new WIconButton(_config, loader.getRequest("icon-stop").bitmap, _colors.semilight, _colors.light, WControls.STOP_CLICKED);
			maximizeBtn = new WIconButton(_config, loader.getRequest("icon-maximize").bitmap, _colors.semilight, _colors.light, WControls.MAXIMIZE_CLICKED);
			minimizeBtn = new WIconButton(_config, loader.getRequest("icon-minimize").bitmap, _colors.semilight, _colors.light, WControls.MINIMIZE_CLICKED);
			sound0Btn = new WIconButton(_config, loader.getRequest("icon-sound_0").bitmap, _colors.semilight, _colors.light, WControls.SOUND0_CLICKED);
			sound1Btn = new WIconButton(_config, loader.getRequest("icon-sound_1").bitmap, _colors.semilight, _colors.light, WControls.SOUND1_CLICKED);
			
			playpauseButton = new WDualStateButton(playBtn, pauseBtn);
			fullscreenButton = new WDualStateButton(maximizeBtn, minimizeBtn);
			soundButton = new WDualStateButton(sound0Btn, sound1Btn);
			
			volumeBar = new WVolumeBar(_config, _colors, ui_config.volume);
			seekBar = new WSeekBar(_config, _colors);
			timeBox = new WTimeBox(_config, _colors);
			
			//	Display Objects
			this.addChild(_bg);
			this.addChild(playpauseButton);
			this.addChild(fullscreenButton);
			this.addChild(soundButton);
			this.addChild(volumeBar);
			this.addChild(timeBox);
			this.addChild(seekBar);
			this.addChild(stopBtn);
		}
		
		public function resize(pWidth:int, pHeight:int):void {
			_bg.graphics.clear();
			_bg.graphics.beginFill(_colors.medium);
			_bg.graphics.drawRect( -_config.border, -_config.border, pWidth + (_config.border * 2), pHeight + (_config.border * 2));
			_bg.graphics.beginFill(_colors.semidark);
			_bg.graphics.drawRect( 0, 0, pWidth, pHeight);
			_bg.graphics.endFill();
			
			//	Position Icons
			playpauseButton.x = 0;
			playpauseButton.y = 0;
			
			stopBtn.x = playpauseButton.x + playpauseButton.width + _config.button_padding;
			stopBtn.y = 0;
			
			seekBar.x = stopBtn.x + stopBtn.width + _config.button_padding;
			seekBar.y = 0;
			seekBar.barWidth = pWidth - (playpauseButton.width + stopBtn.width + fullscreenButton.width + timeBox.width + volumeBar.width + soundButton.width + (_config.button_padding * 6));
			
			timeBox.x = seekBar.x + seekBar.width + _config.button_padding;
			timeBox.y = 0;
			
			soundButton.x = timeBox.x + timeBox.width + _config.button_padding;
			soundButton.y = 0;
			
			volumeBar.x = soundButton.x + soundButton.width + _config.button_padding;
			volumeBar.y = 0;
			
			fullscreenButton.x = volumeBar.x + volumeBar.width + _config.button_padding;
			fullscreenButton.y = 0;
		}
		
		private function _videoStartedHandler(e:Event):void{
			playpauseButton.setState(1);
		}
		
		private function _videoPausedHandler(e:Event):void{
			playpauseButton.setState(0);
		}
		
		private function _videoResumedHandler(e:Event):void{
			playpauseButton.setState(1);
		}
		
		private function _soundEnabledHandler(e:Event):void {
			soundButton.setState(1);
		}
		
		private function _soundDisabledHandler(e:Event):void {
			soundButton.setState(0);
		}
		
		private function _volumeUpdatedHandler(e:Event):void {
			soundButton.setState(1);
		}
	}
}
